package com.coin.controller;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.UserBank;
import com.coin.dto.UserBankDto;
import com.coin.model.R;
import com.coin.service.UserBankService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: 李 力
 * @Date: 2025/6/7
 * @Description: com.coin.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/userBanks")
@Api(tags = "会员的银行卡管理")
public class UserBankController {

    @Autowired
    private UserBankService userBankService;


    /**
     * @Description: 分页查询用户的银行卡
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @GetMapping
    @ApiOperation(value = "分页查询用户的银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "usrId", value = "会员的Id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('user_bank_query')")
    public R<Page<UserBank>> findByPage(Page<UserBank> page, Long usrId) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<UserBank> userBankPage = userBankService.findByPage(page, usrId);
        return R.ok(userBankPage);
    }

    /**
     * @Description: 修改银行卡的状态
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @PostMapping("/status")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "银行卡的Id"),
            @ApiImplicitParam(name = "status", value = "银行卡的状态"),
    })
    @ApiOperation(value = "修改银行卡的状态")
    public R updateStatus(Long id, Byte status) {
        boolean result = userBankService.updateStatus(id, status);
        return result ? R.ok() : R.fail("修改失败");
    }

    /**
     * @Description: 修改银行卡
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @PutMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userBank", value = "银行卡的json数据"),
    })
    @ApiOperation(value = "修改银行卡")
    public R updateStatus(@RequestBody @Validated UserBank userBank) {
        boolean result = userBankService.updateBank(userBank);
        return result ? R.ok() : R.fail("修改失败");
    }

    /**
     * @Description: 绑定银行卡
     * @Author: 李 力
     * @Date: 2025/6/11
     * @Param:
     * @return: R
     */
    @PostMapping("/bind")
    public R bindBank(@RequestBody UserBank userBank) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        boolean result = userBankService.bindBank(userId, userBank);
        return result ? R.ok(true, "绑定成功") : R.fail("绑定失败");
    }

    /**
     * @Description: 远程调用接口
     * @Author: 李 力
     * @Date: 2025/6/19
     * @Param:
     * @return: R
     */
    @GetMapping("/info/{userId}")
    public UserBankDto getUserBankInfo(@PathVariable Long userId) {
        return userBankService.getUserBankInfo(userId);
    }
}
